<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <title>Google Maps AJAX + mySQL/PHP Example</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false"
            type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[
    var map;
    var markers = [];
    var infoWindow;
    var locationSelect;

    function load() {
      map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(40, -100),
        zoom: 4,
        mapTypeId: 'roadmap',
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
      });
      infoWindow = new google.maps.InfoWindow();

      locationSelect = document.getElementById("locationSelect");
      locationSelect.onchange = function() {
        var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
        if (markerNum != "none"){
          google.maps.event.trigger(markers[markerNum], 'click');
        }
      };
   }

   function searchLocations() {
     //var address = document.getElementById("addressInput").value;
     //var geocoder = new google.maps.Geocoder();
     //geocoder.geocode({address: address}, function(results, status) {
       //if (status == google.maps.GeocoderStatus.OK) {
        searchLocationsNear(new google.maps.LatLng(40, -100));
       //} else {
       //  alert(address + ' not found');
       //}
     //});
   }

   function clearLocations() {
     infoWindow.close();
     for (var i = 0; i < markers.length; i++) {
       markers[i].setMap(null);
     }
     markers.length = 0;

     locationSelect.innerHTML = "";
     var option = document.createElement("option");
     option.value = "none";
     option.innerHTML = "See all results:";
     locationSelect.appendChild(option);
   }

   function searchLocationsNear(center) {
     clearLocations(); 

     var radius = document.getElementById('user').value;
     var searchUrl = 'getmarkers.php?user='+radius;
     downloadUrl(searchUrl, function(data) {
	//createMarker(new google.maps.LatLng(40, -100), searchUrl, data);
       var xml = parseXml(data);
       var markerNodes = xml.documentElement.getElementsByTagName("marker");
       var bounds = new google.maps.LatLngBounds();
       for (var i = 0; i < markerNodes.length; i++) {
         var name = markerNodes[i].getAttribute("user");
         var address = markerNodes[i].getAttribute("description");
         var latlng = new google.maps.LatLng(
              parseFloat(markerNodes[i].getAttribute("lat")),
              parseFloat(markerNodes[i].getAttribute("lng")));

         //createOption(name, "1", i);
         createMarker(latlng, name, address);
         //bounds.extend(latlng);
       }
       //map.fitBounds(bounds);
       locationSelect.style.visibility = "visible";
       locationSelect.onchange = function() {
         var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
         google.maps.event.trigger(markers[markerNum], 'click');
       };
      });
    }

    function createMarker(latlng, name, address) {
      var html = "<b>" + name + "</b> <br/>" + address;
      var marker = new google.maps.Marker({
        map: map,
        position: latlng
      });
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
      markers.push(marker);
    }

    function createOption(name, distance, num) {
      var option = document.createElement("option");
      option.value = num;
      option.innerHTML = name + "(" + distance.toFixed(1) + ")";
      locationSelect.appendChild(option);
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request.responseText, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function pushPOI(url) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;
//alert("Bum");
      request.open('GET', url, false);

//request.setRequestHeader("Content-type", "text/xml;charset=UTF-8");
//request.setRequestHeader("From", "morek@interia.pl");
//request.setRequestHeader("SOAPAction","");
//alert(url);
      request.send(null);
//alert("Cyk");
var strResult=request.responseText;

//alert("Cyk"+strResult);
    }

    function parseXml(str) {
      if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
      } else if (window.DOMParser) {
        return (new DOMParser).parseFromString(str, 'text/xml');
      }
    }

    function doNothing() {}
// Removes the overlays from the map, but keeps them in the array
  function clearOverlays() {
    if (markers) {
      for (i in markers) {
        markers[i].setMap(null);
      }
    }
  }

  // Shows any overlays currently in the array
  function showOverlays() {
    if (markers) {
      for (i in markers) {
        markers[i].setMap(map);
      }
    }
  }
function addMarkerClick(){
google.maps.event.addListener(map, 'click', function(event) {
      addMarker(event.latLng);
    });
  }
  
  function addMarker(location) {
google.maps.event.clearListeners(map, 'click');
    if(document.getElementById('markerName').value==""){
        alert("Proszę wypełnić pole Name oraz opcjonalnie Descrition");
    }else{
//alert("Dodaję");
        createMarker(location,document.getElementById('markerName').value,document.getElementById('markerDescription').value);
	pushPOI("getmarkers.php?name="+document.getElementById('markerName').value+"&description="+document.getElementById('markerDescription').value+"&lng="+location.lng()+"&lat="+location.lat()+"&user="+document.getElementById('user').value);
alert("Dodałem");
    }
  }
    //]]>
  </script>
  </head>

  <body style="margin:0px; padding:0px;" onload="load()"> 
    <div>
     User:<input type="text" id="addressInput" size="10"/>
<input type="button" onclick="searchLocations()" value="Search User POI"/>
	Marker Name:<input type="text" id="markerName" size="10"/>
	Marker Description:<input type="text" id="markerDescription" size="15"/>
	<input onclick="clearOverlays();" type=button value="Clear Overlays"/>
    <input onclick="showOverlays();" type=button value="Show All Overlays"/>
	<input onclick="addMarkerClick();" type=button value="Add POI"/>
    
    </div>
    <div><select id="locationSelect" style="width:100%;visibility:hidden"></select></div>
    <div id="map" style="width: 100%; height: 80%"></div>
  </body>
</html>

